﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gomoku
{
    public enum ChessStatus
    {
        None,
        White,
        Black
    }
    class Gomoku
    {
        Chess[,] map = new Chess[GlovalVar.Lines, GlovalVar.Lines];
        public Gomoku(int Lines)
        {
            for (int i = 0; i < GlovalVar.Lines; i++)
            {
                for (int j = 0; j < GlovalVar.Lines; j++)
                {
                    map[i, j] = new Chess(i, j);
                }
            }
        }
        /// <summary>
        /// 取某个坐标棋子的状态
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public ChessStatus GetChessStatus(int x, int y)
        {
            return map[x, y].Status;
        }
        /// <summary>
        /// 设置某个坐标棋子的状态
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="Status">0 - 白色, 1 - 黑色, -1 - 没有棋子</param>
        public void SetChessStatus(int x, int y, ChessStatus Status)
        {
            map[x, y].Status = Status;
        }
        /// <summary>
        /// 判断当前棋盘是否有人获胜，返回游戏状态
        /// 如果还没有棋子成功，那么 StartPosition 和 EndPosition 都为 ChessPosition(-1, -1)
        /// </summary>
        /// <returns></returns>
        public GameStatus GetIsSuccessful()
        {
            GameStatus iss = new GameStatus();
            iss.IsSuccessful = false;
            iss.StartPosition = new ChessPosition(-1, -1);
            iss.EndPosition = new ChessPosition(-1, -1);
            int[,] WhiteChess = new int[GlovalVar.Lines, GlovalVar.Lines], BlackChess = new int[GlovalVar.Lines, GlovalVar.Lines];
            for (int i = 0; i < GlovalVar.Lines; i++)
            {
                for (int j = 0; j < GlovalVar.Lines; j++)
                {
                    Chess c = new Chess(map[i, j].X, map[i, j].Y, map[i, j].Status);
                    if (c.Status == ChessStatus.White)
                    {
                        WhiteChess[i, j] = 1;
                    }
                    else if (c.Status == ChessStatus.Black)
                    {
                        BlackChess[i, j] = 1;
                    }
                }
            }

            /// <summary>
            /// 横排情况
            /// </summary>
            bool MyHorizontal()
            {
                for (int i = 0; i < GlovalVar.Lines - 4; i++)
                {
                    for (int j = 0; j < GlovalVar.Lines; j++)
                    {
                        if (BlackChess[i, j] == 1 && BlackChess[i + 1, j] == 1 && BlackChess[i + 2, j] == 1 && BlackChess[i + 3, j] == 1 && BlackChess[i + 4, j] == 1)
                        {
                            iss.StartPosition = new ChessPosition(i, j);
                            iss.EndPosition = new ChessPosition(i + 4, j);
                            return true;
                        }
                        if (WhiteChess[i, j] == 1 && WhiteChess[i + 1, j] == 1 && WhiteChess[i + 2, j] == 1 && WhiteChess[i + 3, j] == 1 && WhiteChess[i + 4, j] == 1)
                        {
                            iss.StartPosition = new ChessPosition(i, j);
                            iss.EndPosition = new ChessPosition(i + 4, j);
                            return true;
                        }
                    }
                }
                return false;
            }
            /// <summary>
            /// 竖排情况
            /// </summary>
            bool MyVerTical()
            {
                for (int i = 0; i < GlovalVar.Lines; i++)
                {
                    for (int j = 0; j < GlovalVar.Lines - 4; j++)
                    {
                        if (BlackChess[i, j] == 1 && BlackChess[i, j + 1] == 1 && BlackChess[i, j + 2] == 1 && BlackChess[i, j + 3] == 1 && BlackChess[i, j + 4] == 1)
                        {
                            iss.StartPosition = new ChessPosition(i, j);
                            iss.EndPosition = new ChessPosition(i, j + 4);
                            return true;
                        }
                        if (WhiteChess[i, j] == 1 && WhiteChess[i, j + 1] == 1 && WhiteChess[i, j + 2] == 1 && WhiteChess[i, j + 3] == 1 && WhiteChess[i, j + 4] == 1)
                        {
                            iss.StartPosition = new ChessPosition(i, j);
                            iss.EndPosition = new ChessPosition(i, j + 4);
                            return true;
                        }
                    }
                }
                return false;
            }

            /// <summary> 斜线情况
            bool MyOblique()
            {
                for (int i = 0; i < GlovalVar.Lines - 4; i++)
                {
                    for (int j = 0; j < GlovalVar.Lines - 4; j++)
                    {
                        if (BlackChess[i, j] == 1 && BlackChess[i + 1, j + 1] == 1 && BlackChess[i + 2, j + 2] == 1 && BlackChess[i + 3, j + 3] == 1 && BlackChess[i + 4, j + 4] == 1)
                        {
                            iss.StartPosition = new ChessPosition(i, j);
                            iss.EndPosition = new ChessPosition(i + 4, j + 4);
                            return true;
                        }
                        if (WhiteChess[i, j] == 1 && WhiteChess[i + 1, j + 1] == 1 && WhiteChess[i + 2, j + 2] == 1 && WhiteChess[i + 3, j + 3] == 1 && WhiteChess[i + 4, j + 4] == 1)
                        {
                            iss.StartPosition = new ChessPosition(i, j);
                            iss.EndPosition = new ChessPosition(i + 4, j + 4);
                            return true;
                        }
                    }
                }
                return false;
            }
            /// <summary>
            /// 反斜线情况
            /// </summary>
            bool MyBackslash()
            {
                for (int i = GlovalVar.Lines - 1; i >= 4; i--)
                {
                    for (int j = 0; j < GlovalVar.Lines - 4; j++)
                    {
                        //j-(i-1)
                        if (BlackChess[i, j] == 1 && BlackChess[i - 1, j + 1] == 1 && BlackChess[i - 2, j + 2] == 1 && BlackChess[i - 3, j + 3] == 1 && BlackChess[i - 4, j + 4] == 1)
                        {
                            iss.StartPosition = new ChessPosition(i, j);
                            iss.EndPosition = new ChessPosition(i - 4, j + 4);
                            return true;
                        }
                        if (WhiteChess[i, j] == 1 && WhiteChess[i - 1, j + 1] == 1 && WhiteChess[i - 2, j + 2] == 1 && WhiteChess[i - 3, j + 3] == 1 && WhiteChess[i - 4, j + 4] == 1)
                        {
                            iss.StartPosition = new ChessPosition(i, j);
                            iss.EndPosition = new ChessPosition(i - 4, j + 4);
                            return true;
                        }
                    }
                }
                return false;
            }

            if (MyHorizontal() || MyVerTical() || MyOblique() || MyBackslash())
            {
                iss.IsSuccessful = true;
            }
            return iss;
        }
        public Chess[,] GetChessMap()
        {
            return this.map;
        }
    }
    class GameStatus
    {
        public bool IsSuccessful { get; set; }
        public ChessPosition StartPosition { get; set; }
        public ChessPosition EndPosition { get; set; }
        //如果还没有棋子成功，那么 StartPosition 和 EndPosition 都为 ChessPosition(-1, -1)
    }
    class Chess
    {
        public int X { get; set; }
        public int Y { get; set; }
        public ChessStatus Status { get; set; }// 0 - 白色, 1 - 黑色, -1 - 没有棋子
        public Chess(int x, int y)
        {
            X = x; Y = y;
            Status = ChessStatus.None;
        }
        public Chess(int x, int y, ChessStatus status)
        {
            X = x; Y = y;
            Status = status;
        }
    }
    class ChessPosition
    {
        public int X { get; set; }
        public int Y { get; set; }
        public ChessPosition(int x,int y)
        {
            this.X = x;
            this.Y = y;
        }
    }
}